package cn.com.klun.step;

import cn.com.klun.logic.SpiderAllStoInfoLogic;
import cn.com.klun.tjy.base.IStep;
import cn.com.klun.tjy.cache.InitApplicationCache;
import cn.com.klun.tjy.common.constant.ResultEnum;
import cn.com.klun.tjy.common.util.DateUtils;
import cn.com.klun.tjy.dao.TaskInfoDAO;
import cn.com.klun.tjy.dao.entity.TblAllStockInfo;
import cn.com.klun.tjy.dao.entity.TblTaskBasicConfig;
import cn.com.klun.util.IsWeekendUtil;
import cn.com.klun.util.IsWorkDayUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class UpdateStoDayInfoTask implements IStep<TblAllStockInfo> {

    private static Logger logger = LoggerFactory.getLogger(UpdateStoDayInfoTask.class);

    @Autowired
    private SpiderAllStoInfoLogic spiderAllStoInfoLogic;
    @Autowired
    private TaskInfoDAO taskInfoDAO;


    @Override
    public List queryTaskInfo(TblTaskBasicConfig config) {
        // 判断一个日期是否是周六、周日 正式跑时请解开
        String currDate = DateUtils.getCurrDate();
        try {
            if (IsWeekendUtil.isWeekend(currDate)) {
                return null;
            }
            if (!IsWorkDayUtil.isworkday(currDate)) {
                return null;
            }
        } catch (Exception e) {
            logger.error("UpdateStoDayInfoTask.queryTaskInfo判断是否是周六日出错和工作日出错");
        }
        List<TblAllStockInfo> tblAllStockInfos = taskInfoDAO.getAllStockInfo();
        return tblAllStockInfos;
    }

//    @Override
//    public List queryTaskInfo(TblTaskBasicConfig config) {
//        // 判断一个日期是否是周六、周日 正式跑时请解开
//        String currDate = DateUtils.getCurrDate();
//        try {
//            if(IsWeekendUtil.isWeekend(currDate)) {
//                return null;
//            }
//            if(!IsWorkDayUtil.isworkday(currDate)){
//                return null;
//            }
//        } catch (Exception e) {
//            logger.error("UpdateStoDayInfoTask。queryTaskInfo判断是否是周六日出错和工作日出错");
//        }
//        List<TblAllStockInfo> allStockInfo = InitApplicationCache.getAllStockInfo();
//        return allStockInfo;
//    }

    @Override
    public void runTask(TblAllStockInfo tblAllStockInfo) {
        try {
            ResultEnum exec = spiderAllStoInfoLogic.exec(tblAllStockInfo);
            if(!ResultEnum.OK.equals(exec)){
                logger.error("UpdateStoDayInfoTask.runTask tblAllStockInfo:{}error",tblAllStockInfo.getName());
            }
        } catch (Exception e) {
            logger.error(" UpdateStoDayInfoTask.runTask 网页爬取数据处理异常");
        }
    }

    @Override
    public boolean editTaskInfo(List list) {
        return false;
    }

}
